#ifndef MOTION_CONTROLLER_H
#define MOTION_CONTROLLER_H

// provided interface
#include "motctl.h"
// required interface
#include "globaldata.h"
// basic component model
#include <bcm/bcm.h>

namespace cnc {

	namespace motion {

		class MotionController : public bcm::Identifier<bcm::Component>,
			public bcm::Identifier<Control>
		{
			cnc::base::GlobalData *gd_;
		protected:
			MotionController() : bcm::Identifier<bcm::Component>("mc", "Motion Controller"),
				bcm::Identifier<Control>("motctl", "Control"), 
			gd_(0) {}
		protected: // bcm::Component
			virtual bcm::IdentifierData *getProvidedInterfaceTable()
			{
				static bcm::IdentifierData idData[] = {
					{ "motctl", "Control" },
					{ 0, 0 }
				};
				return idData;
			}
			virtual bcm::IdentifierData *getRequiredInterfaceTable()
			{
				static bcm::IdentifierData idData[] = {
					{"gdata", "Global Data"},
					{ 0, 0 }
				};
				return idData;
			}
			virtual bcm::Interface *getInterface(const char *id)
			{
				if (std::string("motctl") == id)
					return static_cast<Control *>(this);
				return 0;
			}
			virtual bool setInterface(Interface *iface)
			{
				if (iface == 0)
					return false;
				if (std::string("gdata") == iface->getID()) {
					cnc::base::GlobalData *gd = dynamic_cast<cnc::base::GlobalData *>(iface);
					if (gd == 0)
						return false;
					gd_ = gd;
				} else
					return false;
				return true;
			}
		protected: //Control
			virtual bool open()
			{
				return true;
			}
			virtual bool close()
			{
				return true;
			}
			virtual void findZero(unsigned int axis){}
		public:
			static MotionController &instance()
			{
				static MotionController object;
				return object;
			}
		};

	} // namespace motctl

} // namespace cnc

#endif

